package com.cqd.mapper;

import com.cqd.entity.Stu;
import com.cqd.service.StuService;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Repository
public class StuServiceImpl implements StuService {

    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public String addStu(Map<String, Object> map, String collectionName) {
        mongoTemplate.save(map,collectionName);
        return "添加成功";
    }

    @Override
    public String updateStu(Query query, Update update, String collectionName) {
        mongoTemplate.updateFirst(query,update,collectionName);
        return "修改成功";
    }

    @Override
    public DeleteResult delStu(Query query, String collectionName) {
        DeleteResult remove = mongoTemplate.remove(query, collectionName);
        return remove;
    }


    @Autowired
    StuMapper stuMapper;

    @Override
    public List<Stu> selectByName(String name) {
        List<Stu> byNameLike = stuMapper.findByNameLike(name);
        return byNameLike;
    }

    @Override
    public List<Stu> selectByAge(Integer age) {
        List<Stu> byAgeIsGreaterThan = stuMapper.findByAgeIsGreaterThan(age);
        return byAgeIsGreaterThan;
    }
}
